import {getQuery} from "../util/util"
const couponModule = {
    namespaced: true,
    state: {
        couponTotal:{currentSkuConpons:[],userConpons:[],innerCouponList:[],skuConpons:[]}
    },
    mutations: {
        addCoupon(state,palyload){
                state.couponTotal.currentSkuConpons=palyload.currentSkuConpons||[]
                state.couponTotal.userConpons=palyload.userConpons||[]
                state.couponTotal.skuConpons=palyload.skuConpons||[]
        }
    },
    actions: {
        //搜索普通优惠券
        searchCommonCoupon({ state, rootState, commit, dispatch }) {
            let cat
            if(rootState.JTTProductInfo.categoryInfo&&rootState.JTTProductInfo.categoryInfo.cid1){
                cat=`${rootState.JTTProductInfo.categoryInfo.cid1},${rootState.JTTProductInfo.categoryInfo.cid2},${rootState.JTTProductInfo.categoryInfo.cid3}`
            }
            else{
                let elementList=Array.from(document.querySelectorAll("#crumb-wrap .crumb .item a"))
                let linkList=elementList.map(item=>item.href)
                let link=linkList[linkList.length-1]
                cat=getQuery(link).cat
            }
            chrome.runtime.sendMessage({
                type:"custom_header",
                url: "/coupon/service",
                baseURL: "https://cd.jd.com",
                params: {
                    skuId:rootState.id,
                    venderId: rootState.productInfo.unionGoods[0][0].vid,
                    cat
                }
            }, function (res) {
                commit("addCoupon",res)
            })
        }
    }
}
export default couponModule